package com.gnerv.sylvanas.business.manage.controller;

import com.gnerv.sylvanas.framework.common.model.RestResult;
import com.gnerv.sylvanas.business.manage.logic.DictLogicService;
import com.gnerv.sylvanas.business.manage.model.dto.DictCreateDTO;
import com.gnerv.sylvanas.business.manage.model.dto.DictUpdateDTO;
import com.gnerv.sylvanas.business.manage.model.vo.DictIndexVO;
import com.gnerv.sylvanas.business.manage.model.vo.DictDetailsVO;
import com.gnerv.sylvanas.business.manage.model.vo.DictVO;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author Gnerv LiGen
 */
@CrossOrigin
@RestController
@AllArgsConstructor
@RequestMapping(value = "/api/manage/dict")
public class DictController {

    private final DictLogicService dictLogicService;

    @GetMapping(value = "/index")
    public RestResult<DictIndexVO> index(@RequestParam(required = false, value = "name") String name,
                                         @RequestParam(required = false, value = "page", defaultValue = "1") int page,
                                         @RequestParam(required = false, value = "size", defaultValue = "30") int size) {
        return dictLogicService.index(name, page, size);
    }

    @PostMapping(value = "")
    public RestResult<Void> create(@RequestBody DictCreateDTO dictCreateDTO) {
        return dictLogicService.create(dictCreateDTO);
    }

    @DeleteMapping(value = "")
    public RestResult<Void> deleteByUkId(@RequestParam(value = "ukId") String ukId) {
        return dictLogicService.deleteByUkId(ukId);
    }

    @PutMapping(value = "")
    public RestResult<Void> updateByUkId(@RequestBody DictUpdateDTO dictUpdateDTO) {
        dictLogicService.updateByUkId(dictUpdateDTO);
        return RestResult.ok();
    }

    @GetMapping(value = "")
    public RestResult<DictVO> selectVOByUkId(@RequestParam(value = "ukId") String ukId) {
        return dictLogicService.selectVOByUkId(ukId);
    }

    @GetMapping(value = "details")
    public RestResult<DictDetailsVO> selectDetailsVOByUkId(@RequestParam(value = "ukId") String ukId) {
        return dictLogicService.selectDetailsVOByUkId(ukId);
    }

    @GetMapping(value = "/details/code")
    public RestResult<DictDetailsVO> selectDetailsVOByCode(@RequestParam(value = "code") String code) {
        return dictLogicService.selectDetailsVOByCode(code);
    }

    @GetMapping(value = "/list")
    public RestResult<List<DictVO>> selectListVOByName(@RequestParam(required = false, value = "name") String name,
                                                       @RequestParam(required = false, value = "page", defaultValue = "1") int page,
                                                       @RequestParam(required = false, value = "size", defaultValue = "30") int size) {
        return dictLogicService.selectListVOByName(name, page, size);
    }

    @GetMapping(value = "/details/list")
    public RestResult<List<DictDetailsVO>> selectListDetailsVOByName(@RequestParam(required = false, name = "name") String name) {
        return dictLogicService.selectListDetailsVOByName(name);
    }

}
